package com.dtw.ditouwang.ui.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.RankingBean;
import com.dtw.ditouwang.models.WebApi;
import com.dtw.ditouwang.repositories.UserRepository;
import com.dtw.ditouwang.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RankingActivity extends BaseActivity implements View.OnClickListener {
    RadioButton radioRun, radioWalk, radioDay, radioMonth, radioWeek;
    TextView textRanking, textName, textDistance;
    ImageView userHead;
    RankingAdapter rankingAdapter;
    List<RankingBean.DataBean.OrderDataBean> orderDataBeanList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking);
        enableBack();
        radioDay = findViewById(R.id.radio_day);
        radioWeek = findViewById(R.id.radio_week);
        radioMonth = findViewById(R.id.radio_month);
        radioRun = findViewById(R.id.radio_run);
        radioWalk = findViewById(R.id.radio_walk);
        textRanking = findViewById(R.id.text_ranking_num);
        textName = findViewById(R.id.text_name);
        textDistance = findViewById(R.id.text_distance);
        userHead = findViewById(R.id.img_head);
        radioDay.setOnClickListener(this);
        radioWeek.setOnClickListener(this);
        radioMonth.setOnClickListener(this);
        radioRun.setOnClickListener(this);
        radioWalk.setOnClickListener(this);
        RecyclerView recyclerView = findViewById(R.id.recycler_ranking);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        rankingAdapter = new RankingAdapter(orderDataBeanList);
        recyclerView.setAdapter(rankingAdapter);
        initDate();
    }

    private void initDate() {
        int movementType = 1, dateType = 1;
        if (radioRun.isChecked()) {
            movementType = 2;
        }
        if (radioWeek.isChecked()) {
            dateType = 2;
        } else if (radioMonth.isChecked()) {
            dateType = 3;
        }
        webApi.getRanking(UserRepository.getInstance().getUserID(), movementType, dateType).enqueue(new Callback<RankingBean>() {
            @Override
            public void onResponse(Call<RankingBean> call, Response<RankingBean> response) {
                if (response.body().isResult()&&!isFinishing()) {
                    textName.setText(response.body().getData().getSelf().getNickname());
                    textDistance.setText(response.body().getData().getSelf().getMileage() + "公里");
                    textRanking.setText(response.body().getData().getSelf().getOrder_number() + "");
                    if (response.body().getData().getSelf().getHead() != null) {
                        Glide.with(userHead).load(WebApi.BaseUrl + response.body().getData().getSelf().getHead().replace("\\", "/")).into(userHead);
                    }
                    orderDataBeanList.clear();
                    orderDataBeanList.addAll(response.body().getData().getOrder_data());
                    rankingAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<RankingBean> call, Throwable t) {

            }
        });

    }

    @Override
    public void onClick(View view) {
        initDate();
    }
}
